<?php

namespace App\Http\Controllers\Admin;
use App\Http\Controllers\Controller;
use App\Http\Controllers\PublicController;

use Illuminate\Http\Request;
use App\Common\ErrorDefine;
use Crypt;
use App\Model\Fg_admin;
class AdminController extends Controller
{
    /**
     * Create a new controller instance.
     *
     * @return void
     */
    private $admin;
    private $public;

    public function __construct(PublicController $public)
    {
        $this->admin = new Fg_admin();
        $this->public = $public;
    }

    /**
     * @param Request $request
     * 管理员列表
     */
    public function getAdminList(Request $request)
    {
        if(!$request->has('token') && $request->input('token') == ''){
            self::showError(ErrorDefine::ACCOUNT_LOGIN_EXPIRED);
            return;
        }

        $data = $this->admin->orderBy('created_at','desc')->get(['id','username','mobile','created_at'])->toArray();
        $count = $this->admin->count();

        return self::ajaxReturn(0,'success',$data,$count);
    }

    /**
     * @param Request $request
     * 添加管理员
     */
    public function addAdmin(Request $request)
    {
        if(empty($request->all()))
        {
            self::ShowError(ErrorDefine::INVALID_PARAMS);
            return;
        }

        if(!$request->has('token') && empty($request->input('token'))){
            self::showError(ErrorDefine::ACCOUNT_LOGIN_EXPIRED);
            return;
        }

        //  用户名
        if(empty($request->input('username')) || !$request->has('username'))
        {
            self::ShowError(ErrorDefine::INVALID_PARAMS);
            return;
        }


        if(empty($request->input('password')) || !$request->has('password'))
        {
            self::ShowError(ErrorDefine::INVALID_PARAMS);
            return;
        }


        if(empty($request->input('mobile')) || !$request->has('mobile'))
        {
            self::ShowError(ErrorDefine::INVALID_PARAMS);
            return;
        }

        $username = $request->input('username');
        $password = $request->input('password');
        $mobile = $request->input('mobile');

        $admin = $this->public->checkAdminExist($username);

        if($admin != NULL){
            self::ShowError(ErrorDefine::ADMIN_EXIST);
            return;
        }

        $params = [
            'username'=>$username,
            'password'=>Crypt::encrypt($password),
            'mobile'=>$mobile
        ];

        try{

            $this->admin->insert($params);

            return self::ajaxReturn();

        }catch (\Exception $exception){

            self::ShowError(ErrorDefine::DATABASE_ERROR);
            return;
        }

    }

    /**
     * @param Request $request
     * 修改管理员基本信息
     */
    public function updateAdmin(Request $request)
    {
        $params =  $request->all();

//        $params['created_at'] =date('Y-m-d H:i:s');

        $name = $request->input('username');

        $data = $this->admin->where('username',$name)->first();

        if(!empty($data))
        {
            $ret = [
                'code'=>ErrorDefine::ADMIN_EXIST,
                'msg'=>''
            ];

            self::showError($ret);
            return;
        }

        try{
            $this->admin->insert($params);
            return self::returnAjax();

        }catch (\Exception $exception){

            self::ShowError(ErrorDefine::DATABASE_ERROR);
            return;
        }

    }


}
